package com.grf.es.highlevelrest;

import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class CreateIndex {

	public void create() throws IOException {
		RestHighLevelClient client = Client.getClient();
//		Map<String, Object> jsonMap = new HashMap();
//		Map<String, Object> idMap = new HashMap();
//		Map<String, Object> properties = new HashMap();
//		idMap.put("type", "keyword");
//		properties.put("id", idMap);
		CreateIndexRequest createIndexRequest = new CreateIndexRequest();
		XContentBuilder mapping = XContentFactory.jsonBuilder()
				.startObject("properties")
					.startObject("id")
					.field("type","keyword")
					.endObject()
				.endObject();
		createIndexRequest.mapping("user",mapping);
	}
	
	
	public static void main(String[] args) throws IOException {
		CreateIndex createIndex = new CreateIndex();
		//createIndex.create();
		createIndex.mapping();
	}
	public void mapping() throws IOException {
//		RestHighLevelClient client = Client.getClient();
//		IndexRequest indexRequest = new IndexRequest("test","user");
//		XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
//				.startObject("mappings")
//					.startObject("properties")
//						.startObject("id")
//							.field("type", "keyword")
//						.endObject()
//					.endObject()
//				.endObject();
//		indexRequest.source(xContentBuilder);
//		client.index(indexRequest);
//		System.out.println("修改成功");
		//创建TransportClient来做，创建新的索引
		Settings settings = Settings.builder().put("cluster.name","my-application")
				.put("client.transport.sniff", false).build();
		TransportClient client = new PreBuiltTransportClient(settings);
		client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("hadoop101"),9300));





	}
}
